package com.qf.cx.simplenms.persist.config;

import java.net.URL;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.hibernate.SessionFactory;
import org.hibernate.dialect.H2Dialect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.qf.cx.simplenms.common.config.CommonConfig;

@Configuration
@EnableTransactionManagement
@EnableAspectJAutoProxy
@ComponentScan(basePackageClasses = { CommonConfig.class }, basePackages = { "com.qf.cx.simplenms.persist" })
@PropertySource("classpath:database.properties")
public class PersistConfig {

	@Autowired
	private Environment env;

	@Bean
	public DataSource hibernateDataSource() {
		BasicDataSource dataSource = new BasicDataSource();
		URL resource = PersistConfig.class.getClassLoader().getResource("");
		dataSource.setDriverClassName("org.h2.Driver");
		dataSource.setUrl("jdbc:h2:" + resource.getPath() + "simplenms;AUTO_SERVER=TRUE");
		dataSource.setUsername(env.getProperty("dbusername"));
		dataSource.setPassword(env.getProperty("dbpassword"));
		return dataSource;
	}

	@Bean
	public LocalSessionFactoryBean hibernateSessionFactory() {
		LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
		sessionFactory.setDataSource(hibernateDataSource());
		sessionFactory.setPackagesToScan(new String[] { "com.qf.cx.simplenms.persist.entity" });
		sessionFactory.setHibernateProperties(hibernateProperties());
		return sessionFactory;
	}

	@Bean
	public HibernateTransactionManager hibernateTransactionManager(@Qualifier(value = "hibernateSessionFactory") SessionFactory sessionFactory) {
		HibernateTransactionManager txManager = new HibernateTransactionManager();
		txManager.setNestedTransactionAllowed(true);
		txManager.setSessionFactory(sessionFactory);
		return txManager;
	}

	private Properties hibernateProperties() {
		Properties properties = new Properties();
		properties.setProperty("hibernate.dialect", H2Dialect.class.getName());
		properties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
		properties.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
		properties.setProperty("hibernate.c3p0.min_size", env.getProperty("hibernate.c3p0.min_size"));
		properties.setProperty("hibernate.c3p0.max_size", env.getProperty("hibernate.c3p0.max_size"));
		properties.setProperty("hibernate.c3p0.timeout", env.getProperty("hibernate.c3p0.timeout"));
		properties.setProperty("hibernate.c3p0.max_statements", env.getProperty("hibernate.c3p0.max_statements"));
		properties.setProperty("hibernate.c3p0.idle_test_period", env.getProperty("hibernate.c3p0.idle_test_period"));
		return properties;
	}
}
